﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models;
using tracker.Models.DTO;
using tracker.Models.BLL;

namespace tracker.Controllers
{
    public class HandlerGetGroupInfoController : Controller
    {
        //
        // GET: /HandlerGetGroupInfo/
        [CallAjaxFilter]
        public JsonStringResult Index(String GroupID)
        {
            string result = "";
            string securityCode = Session[Common.USER_SECUTIRY_CODE].ToString();
            string userType = securityCode.Substring(0, 1).ToUpper();
            List<ENTGroupUserInfo> list = new List<ENTGroupUserInfo>();
            EUserLogin ent =new EUserLoginBLL().getBySecurityCode(GroupID);

            //int flag = 0;

            //if ((userType == "G" && ent.SecurityCode == GroupID) || userType == "M")
            //{
            //    flag = 1;
            //}            

            //if (flag == 1 && ent != null)
            //{
                //vAddToList(list, ent);
            //}
            if (ent != null)
            {
                vAddToList(list, ent);
            }
            result = Utilities.getJsonString(list, "data:", 1);
            result = result.Replace("\"AccountStatusExpression\":null","\"AccountStatusExpression\":"+Utilities.getAccountStatus(ent.Status));
            result = result.Replace("\"PermissionExpression\":null", "\"PermissionExpression\":" + Utilities.getPermission(ent.UserGroupID));
            return new JsonStringResult(result);
        }

        public static void vAddToList(List<ENTGroupUserInfo> list, EUserLogin ent)
        {
            ENTGroupUserInfo entGroupUser = new ENTGroupUserInfo();
            entGroupUser.AccountStatus = ent.Status;
            entGroupUser.Address = ent.Address;
            entGroupUser.CellPhone = ent.Cellphone;
            entGroupUser.Email = ent.Email;
            entGroupUser.EmailWhenAlarm = false;
            entGroupUser.ExpiredTime = ent.EndDate.ToString("yyyy-MM-dd");
            entGroupUser.Name = ent.Fullname;
            entGroupUser.Permisstion = ent.UserGroupID;

            //translateStatusAndPermission( entGroupUser);

            
            entGroupUser.RegisterTime = ent.StartDate.ToString("yyyy-MM-dd");
            entGroupUser.TelePhone = ent.Phone;
            entGroupUser.UserLimit = ent.NumberOfDevice;
            entGroupUser.UserNote = ent.Note;

            list.Add(entGroupUser);
        }

      
    }

    public class ENTGroupUserInfo
    {
        public int AccountStatus { get; set; }
        public string Email { get; set; }
        public string Email2 { get; set; }
        public string Email3 { get; set; }
        public bool EmailWhenAlarm { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
        public string CellPhone { get; set; }
        public string TelePhone { get; set; }
        public string Address { get; set; }
        public int Permisstion { get; set; }
        public string PermissionExpression { get; set; }
        public string RegisterTime { get; set; }
        public string ExpiredTime { get; set; }
        public int UserLimit { get; set; }
        public string AccountStatusExpression { get; set; }
        public string UserNote { get; set; }
    }
}
